﻿#region Opis pliku
//-----------------------------------------------------------------------------
// Buildings.cs
// Autor: Jan Fraś
// 30.10.2012
//-----------------------------------------------------------------------------
#endregion

using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace pogra
{
    /// <summary>
    /// wirtualna klasa budynków. Na razie nic nie robi.
    /// </summary>
    public class Buildings : Entity
    {

        //protected Color color = Color.White;
        protected int spawninterval = 10000;
        protected int time = 0;
        protected int amountToBulid;
        protected Rectangle cliprectengle;
        protected Texture2D texture;//, particletexture;
        protected int kosztProdukcji;
        protected int timeOfConstruction;

        protected enum BulidingState
        {
            Idle,
            Working,
            Construction
        };

        protected BulidingState State; 


        public Buildings(Vector2 pos, float rotation, int team, List<Unit> enemyteam, Texture2D texture) :
            base (pos, rotation, team, enemyteam)
        {
            this.texture = texture;
            cliprectengle = new Rectangle(texture.Width / 2, 0, texture.Width / 2, texture.Height);
            State = BulidingState.Construction;
        }

        public override bool Update(TimeSpan time)
        {
            switch(State)
            {
                case BulidingState.Idle:
                    IdleState(time);
                    break;

                case BulidingState.Working:
                    WorkingState(time);
                    break;

                case BulidingState.Construction:
                    ConstructionState(time);
                    break;
            }

            return base.Update(time);
        }

        public override void Draw(ExtendedSpriteBatch exSpriteBatch)
        {
            if (State == BulidingState.Working) exSpriteBatch.DrawCircle(position, 70, 1, Color.Orange * ((float)time / 100f), Math.PI * 2 * (spawninterval - time) / spawninterval);
            if (State == BulidingState.Construction)
            {
                exSpriteBatch.DrawCircle(position, 70, 1, Color.Black * ((float)lifetime / 100f), Math.PI * 2 * (timeOfConstruction - lifetime) / timeOfConstruction);
            }
            exSpriteBatch.Draw(texture, Position, cliprectengle, color, Rotation, new Vector2(texture.Width / 4, texture.Height / 2), 1, SpriteEffects.None, 0.7f);
           
            float ofset = 30;
            float spaces = 4;
            int length = 8;

            for (int i = 0; i < amountToBulid; i++)
            {
                exSpriteBatch.DrawLine(position + new Vector2(ofset + (float)i * spaces, -ofset), position + new Vector2(ofset + (float)i * spaces, -ofset + length), Color.Red,2);
            }

                base.Draw(exSpriteBatch);
        }

        virtual protected void ConstructionState(TimeSpan time)
        {
            color = Color.White * (float)((lifetime + 0.5 * timeOfConstruction) / timeOfConstruction);
            if (lifetime > timeOfConstruction)
            {
                State = BulidingState.Idle;
                cliprectengle = new Rectangle(0, 0, texture.Width / 2, texture.Height);
            }
        }

        virtual protected void WorkingState(TimeSpan time)
        {
            this.time += time.Milliseconds;
            if (amountToBulid == 0)
            {
                State = BulidingState.Idle;
            }
        }


        virtual protected void IdleState(TimeSpan time)
        {
            if (amountToBulid != 0)
                State = BulidingState.Working;
        }

        /// <summary>
        /// składanie zmówienia na jednostki. 
        /// Pierwszy parametr to referencja do portfela zamawiającego, drugi towielkość zamówienia. Defaultowo - 1;
        /// </summary>
        /// <param name="kasa"></param>
        /// <param name="order"></param>
        public void MakeAnOrder(ref int kasa, int order = 1)
        {
            if (kasa >= kosztProdukcji * order)
            {
                amountToBulid += order;
                kasa -= kosztProdukcji;
            }
            else
            {
                int tmp = kasa / kosztProdukcji;             
                amountToBulid += tmp;
                kasa -= tmp * kosztProdukcji;
            }
        }

    }
}
